#!/bin/bash

##################################################
# find keyword from $2 with $1
# example)
# FILE='build.json'
# url_adminurl=$(find_keyword 'url.adminurl' $FILE)
###################################################
function find_keyword()
{
	cmd=$(jq .$1 $2 | cut -d "\"" -f 2)
	echo $cmd
}

###################################################
# replace $1 to $2 in $3
# example)
# replace "%SERVER%" $url_adminurl test.conf
###################################################
function replace()
{
	msg="s/$1/$2/g"
	sed -i $msg $3
}

###################################################
# get file($3) from url($2)+($3)
# example)
# get_file "/etc/nova/nova.conf" "http://example.com" "/etc/nova/nova.conf.sample"
###################################################
function get_file()
{
	rm -f $1
	wget -O $1 $2$3
}

# Shortcut function to get a newly generated ID
# ex) ADMIN_TENANT=$(keystone tenant-create --name=admin | grep " id " | get_field 2)
function get_field() {
    while read data; do
        if [ "$1" -lt 0 ]; then
            field="(\$(NF$1))"
        else
            field="\$$(($1 + 1))"
        fi
        echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
    done
}

function display() 
{
	sleep 2
	clear
	echo "#########################################################"
	echo " $1 "
	echo "---------------------------------------------------------"
}

function check_tools()
{
	display "Check tools..."
	if command -v jq ; then
		echo "[jq] json parser exist"
	else
		echo "[jq] json parser does not exist"
		apt-get install -y jq
	fi

	apt-get update
}
function initial_config()
{
        echo "going..."
        apt-get update

        msg="s/127.0.1.1/$1/g"
        sed -i $msg /etc/hosts

        echo "
        net.ipv4.ip_forward=1
        net.ipv4.conf.all.rp_filter=0
        net.ipv4.conf.default.rp_filter=0
        " >> /etc/sysctl.conf
}

function install_db()
{
	display "Install MySQL Server"
	
	CONF="/etc/mysql/my.cnf"
	NODE_MYSQL=$(find_keyword 'node.mysql' $1)
	
	apt-get install -y python-mysqldb mysql-server
	get_file $CONF $URL $CONF
	replace "%SERVER%" $NODE_MYSQL $CONF
	
	service mysql restart
	mysql_install_db
	mysql_secure_installation
}

function drop_db()
{
        display "Drop OpenStack DB"
mysql -u root -p  <<EOF
DROP DATABASE nova;
DROP DATABASE glance;
DROP DATABASE keystone;
DROP DATABASE neutron;
DROP DATABASE cinder;
FLUSH PRIVILEGES;
EOF
}

function update_db()
{
        display "Update OpenStack DB"
	NOVA_DBPASS=$(find_keyword 'db_password.nova' $1)
	GLANCE_DBPASS=$(find_keyword 'db_password.glance' $1)
	KEYSTONE_DBPASS=$(find_keyword 'db_password.keystone' $1)
	NEUTRON_DBPASS=$(find_keyword 'db_password.neutron' $1)
	CINDER_DBPASS=$(find_keyword 'db_password.cinder' $1)

	echo $NOVA_DBPASS
	echo $GLANCE_DBPASS
	echo $KEYSTONE_DBPASS
	echo $NEUTRON_DBPASS
	echo $CINDER_DBPASS
	

mysql -u root -p <<EOF
CREATE DATABASE nova;
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'%' \
IDENTIFIED BY '$NOVA_DBPASS';
CREATE DATABASE glance;
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' \
IDENTIFIED BY '$GLANCE_DBPASS';
CREATE DATABASE keystone;
GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' \
IDENTIFIED BY '$KEYSTONE_DBPASS';
CREATE DATABASE neutron;
GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'%' \
IDENTIFIED BY '$NEUTRON_DBPASS';
CREATE DATABASE cinder;
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'%' \
IDENTIFIED BY '$CINDER_DBPASS';
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'localhost' \
IDENTIFIED BY '$NOVA_DBPASS';
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' \
IDENTIFIED BY '$GLANCE_DBPASS';
GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' \
IDENTIFIED BY '$KEYSTONE_DBPASS';
GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'localhost' \
IDENTIFIED BY '$NEUTRON_DBPASS';
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'localhost' \
IDENTIFIED BY '$CINDER_DBPASS';
FLUSH PRIVILEGES;
EOF
}

function install_mq()
{
        display "Install Rabbit Server"
	RABBIT_PASS=$(find_keyword 'mq.password' $1)
        sudo apt-get install -y rabbitmq-server
        sudo rabbitmqctl change_password guest $RABBIT_PASS
}


function install_keystone()
{
	NODE_KEYSTONE=$(find_keyword 'node.keystone' $1)
        display "Install Keystone at $NODE_KEYSTONE"

        sudo apt-get install -y keystone python-keystone python-keystoneclient

        CONF="/etc/keystone/keystone.conf"
        get_file $CONF $URL $CONF
        replace "%SERVER%" $NODE_KEYSTONE $CONF

        su -s /bin/sh -c "keystone-manage db_sync" keystone

        service keystone restart
}

function update_keystone()
{
	CONF=/root/openrc
	NODE_KEYSTONE=$(find_keyword 'node.keystone' $1)
	get_file $CONF $URL $CONF
        display "Update openrc at $CONF with $NODE_KEYSTONE"
	replace "%SERVER%" $NODE_KEYSTONE $CONF

	source $CONF
        echo "source $CONF" >> ~/.bashrc

        echo "DEBUG..."
        echo $OS_SERVICE_TOKEN
        echo $OS_SERVICE_ENDPOINT
        echo "END debug..."
        sleep 5

        echo "Update keystone-init"
        CONF=/root/keystone-init
	get_file $CONF $URL $CONF
	ADMIN_PASS=$(find_keyword 'user_password.admin' $1)
	DEMO_PASS=$(find_keyword 'user_password.demo' $1)
	NOVA_PASS=$(find_keyword 'user_password.nova' $1)
	GLANCE_PASS=$(find_keyword 'user_password.glance' $1)
	KEYSTONE_PASS=$(find_keyword 'user_password.keystone' $1)
	NEUTRON_PASS=$(find_keyword 'user_password.neutron' $1)
	CINDER_PASS=$(find_keyword 'user_password.cinder' $1)

	ADMIN_EMAIL=$(find_keyword 'user_email.admin' $1)
	DEMO_EMAIL=$(find_keyword 'user_email.demo' $1)
	NOVA_EMAIL=$(find_keyword 'user_email.nova' $1)
	GLANCE_EMAIL=$(find_keyword 'user_email.glance' $1)
	KEYSTONE_EMAIL=$(find_keyword 'user_email.keystone' $1)
	NEUTRON_EMAIL=$(find_keyword 'user_email.neutron' $1)
	CINDER_EMAIL=$(find_keyword 'user_email.cinder' $1)

	ADMINURL=$(find_keyword 'url.adminurl' $1)
	PUBLICURL=$(find_keyword 'url.publicurl' $1)
	INTERNALURL=$(find_keyword 'url.internalurl' $1)

	replace "%SERVER%" $NODE_KEYSTONE $CONF
	replace "%ADMIN_PASS%" $ADMIN_PASS $CONF
	replace "%DEMO_PASS%" $DEMO_PASS
	replace "%NOVA_PASS%" $NOVA_PASS
	replace "%GLANCE_PASS%" $GLANCE_PASS
	replace "%KEYSTONE_PASS%" $KEYSTONE_PASS
	replace "%NEUTRON_PASS%" $NEUTRON_PASS
	replace "%CINDER_PASS%" $CINDER_PASS

	replace "%ADMIN_EMAIL%" $ADMIN_EMAIL $CONF
	replace "%DEMO_EMAIL%" $DEMO_EMAIL
	replace "%NOVA_EMAIL%" $NOVA_EMAIL
	replace "%GLANCE_EMAIL%" $GLANCE_EMAIL
	replace "%KEYSTONE_EMAIL%" $KEYSTONE_EMAIL
	replace "%NEUTRON_EMAIL%" $NEUTRON_EMAIL
	replace "%CINDER_EMAIL%" $CINDER_EMAIL


	replace "%ADMINURL%" $ADMINURL
	replace "%PUBLICURL%" $PUBLICURL
	replace "%INTERNALURL%" $INTERNALURL


        chmod 755 $CONF
        $CONF
}

echo "===================== OpenStack Deploy Model ======================="
echo "                                                                    "
echo "                      (Internet)      (Managment Network)           "
echo "                          |                     |                   "
echo "                    -------------+--           ---+------------     "
echo "                                 |                |                 "
echo "                             +--eth1-------------eth0---+           "
echo "                             |                          |           "
echo "                             |  Nova-API, Nova-Conductor|           "
echo "                             |  Nova Scheduler          |           "
echo "                             |  Neutron Server          |           "
echo "                             |  Cinder-API,Cinder-volume|           "
echo "                             +--------------------------+           "
echo "                                                                    "
echo "                                                                    "
echo "Usage:openstack_install.sh <managemnt IP>                           "
echo "      ex) openstack_install.sh 192.168.1.1                          "
echo "Are you specify host IP($1)?                                        "
echo "===================================================================="

sleep 5
echo "The number of arguments is: $#"

if [ $# -eq 1 ]
then
        echo "success"
else
        echo "fail"
        exit
fi


URL="https://conf.googlecode.com/svn/trunk/openstack/icehouse"
CONF=$1

#check_tools
#install_db $CONF
#update_db $CONF 
install_mq $CONF
install_keystone $CONF
